class_name HealthComponent
extends Node

## 单位受伤
signal unit_hit
## 单位死亡
signal unit_died
## 生命值变化
signal health_change(current_val: int, max_val: int)


var max_health: int = 1
var current_health: int = 1



func setup(unit_stats: UnitStats):
	max_health = unit_stats.health
	current_health = max_health
	health_change.emit(current_health, max_health)


## 造成伤害
func take_damage(value: int):
	var current_value := clampi(current_health - value, 0, max_health)
	if current_health != current_value:
		current_health = current_value
		health_change.emit(current_health, max_health)
	unit_hit.emit()
	if current_health == 0:
		unit_died.emit()


func heal(value: int):
	if current_health == 0:
		return
	var current_value := clampi(current_health + value, 0, max_health)
	if current_health != current_value:
		current_health = current_value
		health_change.emit(current_health, max_health)
